#include "tddl_cipher.h"
#include "platform.h"
#include "indicate.h"
#include "stdio.h"
#include "string.h"


void tddl_cipher_sha1Test() {
    char message[] = "abc";
    UINT32 res[5];
    int i;
	DataBlock sourceData;
	DataBlock resultData;
	
    printf("\n-------------------tddl_cipher_sha1Test---------------------\n");
	
	sourceData.dataLength = 3;
	sourceData.data = message;
	
	tddl_cipher_doSHA1(&sourceData, &resultData);
	printf ("tddl resultData: \n");
    for(i = 0; i < resultData.dataLength; i++)
        printf("%02X ", *(resultData.data + i));
	printf ("\n");
	// free(sourceData);
	// free(resultData);
    //printf("\nShould Match\nA9993E36 4706816A BA3E2571 7850C26C 9CD0D89D\n");
}

void tddl_cipher_aesTest() {
	int i;
	DataBlock key;
	DataBlock sourceData;
	DataBlock resultData;
	DataBlock plainData;
	
	//生成密钥
	printf("\n-------------------tddl_cipher_generateAESKey128---------------------\n");
    tddl_cipher_generateAESKey128(&key);
    printf("The key is: \n");
    for(i = 0; i < key.dataLength; i++) {
        printf("%02x", *(key.data + i));
    }
	printf("\n");
	
	//AES加密
	printf("\n-------------------tddl_cipher_AESEncipher128---------------------\n");
	sourceData.dataLength = 16;
	sourceData.data = (BYTE *)malloc(sourceData.dataLength);
    for(i = 0; i < sourceData.dataLength; i++) {
        *(sourceData.data + i) = (UINT8)i;
    }
    printf("\nThe plain is: ");
    for(i = 0; i < sourceData.dataLength; i++) {
        printf("%02x", *(sourceData.data + i));
    }
	printf ("\n");
    tddl_cipher_AESEncipher128(&key, &sourceData, &resultData);
    printf("\nThe cipher is: ");
    for(i = 0; i < resultData.dataLength; i++) {
        printf("%02x", *(resultData.data + i));
    }
	printf ("\n");

	//AES解密
	printf("\n-------------------tddl_cipher_AESDecipher128---------------------\n");
    tddl_cipher_AESDecipher128(&key, &resultData, &plainData);
    printf("\nThe rePlainText is: ");
    for(i = 0; i < plainData.dataLength; i++) {
        printf("%02x", *(plainData.data + i));
    }
    printf("\n");
	
}

void tddl_cipher_pcr_test(){
	DataBlock sourceData, resultData;
	int i;
	
	//读PCR
	printf("\n-------------------tddl_cipher_readPCR---------------------\n");
	tddl_cipher_readPCR(1, &resultData);
	printf ("pcr resultData is :\n");
	for (i=0; i<resultData.dataLength; i++){
		printf("%02x ", *(resultData.data + i));
	}
	printf ("\n");
	
	
	//写PCR
	printf("\n-------------------tddl_cipher_writePCR---------------------\n");
	sourceData.dataLength = 20;
	sourceData.data = (BYTE *)malloc(sourceData.dataLength);
	for (i=0; i<sourceData.dataLength; i++){
		//printf("%02x ", *(resultData.data + i));
		*(sourceData.data + i) = (UINT8)i;
	}
	
	tddl_cipher_writePCR(1, &sourceData);
	
	//读PCR
	printf("\n-------------------tddl_cipher_readPCR---------------------\n");
	tddl_cipher_readPCR(1, &resultData);
	printf ("pcr resultData is :\n");
	for (i=0; i<resultData.dataLength; i++){
		printf("%02x ", *(resultData.data + i));
	}
	printf ("\n");
}

void tddl_cipher_random_test() {
	DataBlock resultData;
	int i;
	
	//获取随机数
	printf("\n-------------------tddl_cipher_writePCR---------------------\n");
	tddl_cipher_getRandom(8, &resultData);
	printf ("pcr resultData is :\n");
	for (i=0; i<resultData.dataLength; i++){
		printf("%02x ", *(resultData.data + i));
	}
	printf ("\n");
	
}

void tddl_cipher_rsa_test() {
	DataBlock key, sourceData, resultData, plainData;
	int i;
	
	//生成密钥
	printf("\n-------------------tddl_cipher_RSA_keyGen---------------------\n");
    tddl_cipher_RSA_keyGen(&key);
    printf("The key is: \n");
    for(i = 0; i < key.dataLength; i++) {
        printf("%02x", *(key.data + i));
    }
	printf("\n");
	
	//RSA加密
	printf("\n-------------------tddl_cipher_RSA_encipher---------------------\n");
	sourceData.dataLength = 16;
	sourceData.data = (BYTE *)malloc(sourceData.dataLength);
    for(i = 0; i < sourceData.dataLength; i++) {
        *(sourceData.data + i) = (UINT8)i;
    }
    printf("\nThe plain is: ");
    for(i = 0; i < sourceData.dataLength; i++) {
        printf("%02x", *(sourceData.data + i));
    }
	printf ("\n");
    tddl_cipher_RSA_encipher(&key, &sourceData, &resultData);
    printf("\nThe cipher is: ");
    for(i = 0; i < resultData.dataLength; i++) {
        printf("%02x", *(resultData.data + i));
    }
	printf ("\n");

	//RSA解密
	printf("\n-------------------tddl_cipher_RSA_decipher---------------------\n");
    tddl_cipher_RSA_decipher(&key, &resultData, &plainData);
    printf("\nThe rePlainText is: ");
    for(i = 0; i < plainData.dataLength; i++) {
        printf("%02x", *(plainData.data + i));
    }
    printf("\n");
	
}


void tddl_cipher_file_sha1Test() {

    UINT32 res[5];
    int i;
	DataBlock sourceData;
	DataBlock resultData;
	FILE * fp;
	char strLine[1024];
	
	if ((fp = fopen("/ram/aes.txt", "r")) == NULL){
		printf ("error!");
		return;
	}

	
	while (!feof(fp)){
		printf("\n-------------------tddl_cipher_sha1Test---------------------\n");
		fgets(strLine, 1024, fp);
		// printf ("%s\n", strLine);
		sourceData.dataLength = strlen(strLine);
		sourceData.data = (BYTE *)malloc(sourceData.dataLength);
		for (i = 0; i < sourceData.dataLength; i++){
			*(sourceData.data + i) = (UINT8)strLine[i];
			//printf ("%c ", strLine[i]);
		}
		tddl_cipher_doSHA1(&sourceData, &resultData);
		printf ("tddl resultData: \n");
		for(i = 0; i < resultData.dataLength; i++)
			printf("%02X ", *(resultData.data + i));
		printf ("\n");
	}
	
    
	
	// sourceData.dataLength = 3;
	// sourceData.data = message;
	
	
	// free(sourceData);
	// free(resultData);
    //printf("\nShould Match\nA9993E36 4706816A BA3E2571 7850C26C 9CD0D89D\n");
}

/*void tddl_cipher_aesTest() {
    const int blockSize = 16;
    UINT8 key[blockSize];
    UINT8 plainText[256];
    UINT8 cipherText[256];
    UINT8 rePlainText[256];
    const int plainLength = 20;
    int i, j, count;
    count = (plainLength - 1) / blockSize + 1;

    printf("\nTDDL_TestAES\n");
    tddl_cipher_generateAESKey128(key);
    printf("The key is: \n");
    for(i = 0; i < blockSize; i++) {
        printf("%02x", key[i]);
    }
	

	for(i = 0; i < blockSize; i++) {
        printf("%02x", key[i]);
    }

    for(i = 0; i < plainLength; i++) {
        plainText[i] = (UINT8)(i + 2);
    }
    printf("\nThe plain is: ");
    for(i = 0; i < plainLength; i++) {
        if(i % blockSize == 0)
            printf("\n");
        printf("%02x", plainText[i]);
    }

    tddl_cipher_AESEncipher128(plainText, plainLength, key, cipherText);
    printf("\nThe cipher is: ");
    for(i = 0; i < count; i++) {
        printf("\n");
        for(j = 0; j < blockSize; j++) {
            printf("%02x", cipherText[i * blockSize + j]);
        }
    }

    tddl_cipher_AESDecipher128(cipherText, plainLength, key, rePlainText);
    printf("\nThe rePlainText is: ");
    for(i = 0; i < plainLength; i++) {
        if(i % blockSize == 0)
            printf("\n");
        printf("%02x", rePlainText[i]);
    }
    printf("\n");
}*/

void tddl_cipher_test() {
    tddl_cipher_sha1Test();
    tddl_cipher_aesTest();
	tddl_cipher_pcr_test();
	tddl_cipher_random_test();
	tddl_cipher_rsa_test();
}


